//
//  ONViewController.m
//  PPL
//
//  Created by Familly on 3/25/14.
//  Copyright (c) 2014 Familly. All rights reserved.
//

#import "ONViewController.h"
#import "QuizViewController.h"
//#import "MCProgressBarView.h"

@interface ONViewController () <UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate> {
   
    UIImageView * _backgroundImageView;
    UIImageView * _foregroundImageView;
    CGFloat _minimumForegroundWidth;
    CGFloat _availableWidth;
    CALayer * _maskLayer;
}

@end

@implementation ONViewController
@synthesize strMode;
@synthesize strQuestions;

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
   
    if(pickerView.tag ==1)
    {
        self.modeIndex = row;
        strMode = [self.arrMode objectAtIndex:self.modeIndex];


    }
    if (pickerView.tag == 2) {
        self.chapIndex = row;
        
    }
    else
    {
        self.questIndex = row;
        strQuestions = [self.arrQuestion objectAtIndex:self.questIndex];
    }
}

-(IBAction)touchStart:(id)sender
{
    UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];
    QuizViewController *saveMode = [mystoryboard instantiateViewControllerWithIdentifier:@"QuizViewController"];
    
    saveMode.modestring = strMode;
    saveMode.questionsString = strQuestions;
    
    [self.navigationController pushViewController:saveMode animated:NO];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    //data for picker
    _arrMode = @[@"Learn", @"Test", @"Exam"];
    _arrChapter = @[@"Weather", @"Air Law", @"Navigation"];
    _arrQuestion = @[@"15", @"20", @"All"];
    
    [self.modePicker selectRow:1 inComponent:0 animated:YES];
    [self.chapterPicker selectRow:1 inComponent:0 animated:YES];
    [self.questionsPicker selectRow:1 inComponent:0 animated:YES];
    
    //progress bar
    UIImage * backgroundImage = [[UIImage imageNamed:@"progress-bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
    UIImage * foregroundImage = [[UIImage imageNamed:@"progress-fg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];

    [self processBarBackgroundImage:backgroundImage foregroundImage:foregroundImage];
    self.strMode = [[NSString alloc] init];
    strMode = [self.arrMode objectAtIndex:1];
    strQuestions = [self.arrQuestion objectAtIndex:1];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    if (pickerView.tag == 1) {
        return 80;
    }
    if (pickerView.tag == 2) {
        return 150;
    }
    if (pickerView.tag == 3 ) {
        return 40;
    }
    return 0;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma -mark Progressbar
//===========create progressbar

- (void)processBarBackgroundImage:(UIImage *)backgroundImage foregroundImage:(UIImage *)foregroundImage
{
        _backgroundImageView = [[UIImageView alloc] initWithFrame:self.progressBarImage.bounds];
        _backgroundImageView.image = backgroundImage;
        [self.progressBarImage addSubview:_backgroundImageView];
        
        _foregroundImageView = [[UIImageView alloc] initWithFrame:self.progressBarImage.bounds];
        _foregroundImageView.image = foregroundImage;
        [self.progressBarImage addSubview:_foregroundImageView];
        
        _maskLayer = [CALayer layer];
        _maskLayer.contents = (id)[self generateMaskImageFromImage:foregroundImage forSize:self.progressBarImage.frame.size].CGImage;
        _maskLayer.frame = (CGRect){CGPointZero, self.progressBarImage.frame.size};
        _foregroundImageView.layer.mask = _maskLayer;
        
        UIEdgeInsets capInsets = foregroundImage.capInsets;
        self.offsetForZero = capInsets.left + capInsets.right;
//        self.progress = 0.5;
        [self setProgress:0.75];
}



- (UIImage *)generateMaskImageFromImage:(UIImage *)image forSize:(CGSize)size
{
    CGFloat scale = image.scale;
    
    UIGraphicsBeginImageContextWithOptions(size, NO, scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    CGImageRef cgImage = CGBitmapContextCreateImage(context);
    UIImage *outImage = [UIImage imageWithCGImage:cgImage scale:scale orientation:UIImageOrientationUp];
    
    CGImageRelease(cgImage);
    UIGraphicsEndImageContext();
    
    return outImage;
}

- (void)setOffsetForZero:(CGFloat)offsetForZero
{
    _offsetForZero = offsetForZero;
    self.progress = self.progress;
}

- (void)setProgress:(double)progress
{
    _progress = progress;
    CGRect frame = _maskLayer.frame;
    frame.origin.x = (frame.size.width - _offsetForZero) * (_progress - 1.0);
    _maskLayer.frame = frame;
}
//=========done create progress bar

#pragma -mark Picker
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
    
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (pickerView == self.modePicker) {
        return _arrMode.count;
    }
    if (pickerView == self.chapterPicker) {
        return _arrChapter.count;
    }
    else
        return _arrQuestion.count;
    
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) component {
    if (pickerView == self.modePicker) {
        return _arrMode[row];
    }
    if (pickerView == self.chapterPicker) {
        return _arrChapter[row];
    }
    else
        return _arrQuestion[row];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UIView *view in self.view.subviews) {
        [view resignFirstResponder];
    }
}
@end

